package android.vstore.activities;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.appvn2014.appstorevn.R;

import android.R.anim;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.SearchView;
import android.widget.TextView;
import anroid.vstore.provider.SharedPreference;

public class BaseActivity extends BaseFlurryFragmentActivity {
	private AlertDialog alertDialog;
	boolean isCheckedNotification;
	protected int NOTIFICATION_COMING;
	protected Integer listNotf;
	private View lay_noti_count;
	private TextView tv_notif_count;
	private FrameLayout fl_notiLay;
	private MenuItem mitem_notification;

	protected ActionBar actionBar;
	private boolean isVisible;
	@Override
	protected void onStart() {
	  super.onStart();
	  setVisible(true);
	}
	@Override
	protected void onResume() {
	  super.onResume();
	  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	  setVisible(true);
	}

	@Override
	protected void onPause() {
	  super.onPause();
	  setVisible(false);
	}
	@Override
	protected void onStop() {
	  super.onStop();
	  setVisible(false);
	}
	
	@SuppressLint("NewApi")
	protected void InitActionBar() {
		if(android.os.Build.VERSION.SDK_INT >= 11){
			// get the action bar
			actionBar = getActionBar();
			actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main)));
			// Enabling Back navigation on Action Bar icon
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setNavigationMode(ActionBar.DISPLAY_HOME_AS_UP);
		}
	}
	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(android.os.Build.VERSION.SDK_INT>= 11)
		{
			// Inflate the menu; this adds items to the action bar if it is present.
			getMenuInflater().inflate(R.menu.scene_group, menu);
			lay_noti_count = menu.findItem(R.id.action_notification).getActionView();

			lay_noti_count.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					MainActivity.isCheckedNotification= true;

					SharedPreference sharedpref = new SharedPreference(BaseActivity.this);
					DateFormat df = new SimpleDateFormat("dd MM yyyy");
					Date currDate = Calendar.getInstance().getTime();
					sharedpref.setLastestDate(df.format(currDate));

					Intent intent = new Intent(BaseActivity.this, SceneNotificationsActivity.class);
					startActivityForResult(intent,11);
				}
			});


			tv_notif_count = (TextView) lay_noti_count.findViewById(R.id.tv_mainview_number_notification);
			fl_notiLay = (FrameLayout) lay_noti_count.findViewById(R.id.fl_mainview_notification);
			//		count.setVisibility(View.INVISIBLE);
			//		
			if(isCheckedNotification)
				fl_notiLay.setVisibility(View.INVISIBLE	);
			mitem_notification = menu.findItem(R.id.action_notification);
			mitem_notification.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

			SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
			SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
					.getActionView();
			searchView.setSearchableInfo(searchManager
					.getSearchableInfo(getComponentName()));
			Load();
		}
		return true;
	}
	public void Load()
	{
		listNotf = MainActivity.listnotf;
		_messageHandler.sendMessage(Message.obtain(_messageHandler, NOTIFICATION_COMING, 0, 0, listNotf));

	}
	public Handler _messageHandler = new Handler(){


		@Override
		public void handleMessage(Message msg)
		{
			if(msg.what == NOTIFICATION_COMING)
			{

				if(listNotf == 0 || MainActivity.isCheckedNotification)
					fl_notiLay.setVisibility(View.INVISIBLE);
				//					notification.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
				else
				{
					fl_notiLay.setVisibility(View.VISIBLE);
					//					notification.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
					tv_notif_count.setText(listNotf + "");
				}
			}


		}
	};
	// This is base class for almost activities.
	public void ShowUpdatingFunction()
	{
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(getString(R.string.missing_func_title));
		alertDialog.setMessage(getString(R.string.missing_func_body));
		alertDialog.setButton(getString(R.string.dialog_yes), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.dismiss();
			}
		});
		alertDialog.setIcon(R.drawable.ic_launcher);
		alertDialog.show();
	}
	public boolean isVisible() {
		return isVisible;
	}
	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}
}
